import { PKG_NAME, PKG_PREFIX } from "../utils/paths";

import type { Plugin } from "rollup";

export function pathAlias(): Plugin {
  const themeChalk = "theme-chalk";
  const sourceThemeChalk = `${PKG_PREFIX}/${themeChalk}` as const;
  const bundleThemeChalk = `${PKG_NAME}/${themeChalk}` as const;

  return {
    name: "path-alias-plugin",
    resolveId(id) {
      if (!id.startsWith(sourceThemeChalk)) return;
      return {
        id: id.replace(new RegExp(sourceThemeChalk, "g"), bundleThemeChalk),
        external: "absolute",
      };
    },
  };
}
